﻿using AutoMapper;
using Itech_Run.Application.Contracts.Shared.Dto;
using Itech_Run.Blog.Application.Contracts.Dto;
using Itech_Run.Blog.Domain.Entity;

namespace Itech_Run.Blog.Application
{
    public class Itech_RunBlogApplicationAutoMapperProfile : Profile
    {
        public Itech_RunBlogApplicationAutoMapperProfile()
        {
            CreateMap<Blog_Info, BlogInfoDto>().ForMember(dest => dest.ReadNum, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_Interest != null)
                        return blog.Blog_Interest.ReadNum;
                    return 0;
                });
            }).ForMember(dest => dest.CommentNum, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    return blog.Blog_Comment.Count;
                });
            });

            CreateMap<BlogInfoCreateDto, Blog_Info>();

            CreateMap<BlogInfoUpdateDto, Blog_Info>();

            CreateMap<Blog_Info, BlogInfoVerboseDto>().ForMember(dest => dest.ReadNum, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_Interest != null)
                        return blog.Blog_Interest.ReadNum;
                    return 0;
                });
            }).ForMember(dest => dest.CommentNum, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    return blog.Blog_Comment.Count;
                });
            }).ForMember(dest => dest.PersonCategoryList, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_PersonCategory_Map != null)
                        return blog.Blog_PersonCategory_Map.Select(s => s.PersonCategoryId);
                    return new List<string>();
                });
            }).ForMember(dest => dest.PersonLabelList, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_PersonLabel_Map != null)
                        return blog.Blog_PersonLabel_Map.Select(s => s.PersonLabelId);
                    return new List<string>();
                });
            });

            #region 前端
            CreateMap<Blog_Info, Front_BlogInfoDto>().ForMember(dest => dest.ReadNum, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_Interest != null)
                        return blog.Blog_Interest.ReadNum;
                    return 0;
                });
            }).ForMember(dest => dest.CommentNum, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    return blog.Blog_Comment.Count;
                });
            }).ForMember(dest => dest.PersonCategoryList, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_PersonCategory_Map != null)
                        return blog.Blog_PersonCategory_Map.Select(s => new KeyValueDto
                        {
                            Key = s.PersonCategory.Id,
                            Value = s.PersonCategory.Name
                        });
                    return new List<KeyValueDto>();
                });
            }).ForMember(dest => dest.PersonLabelList, options =>
            {
                options.MapFrom((blog, blogDto) =>
                {
                    if (blog.Blog_PersonLabel_Map != null)
                        return blog.Blog_PersonLabel_Map.Select(s => new KeyValueDto
                        {
                            Key = s.PersonLabel.Id,
                            Value = s.PersonLabel.Name
                        });
                    return new List<KeyValueDto>();
                });
            });

            CreateMap<Blog_Comment, Front_BlogCommentDto>();
            CreateMap<BlogCommentCreateDto, Blog_Comment>();
            #endregion
        }
    }
}
